package com.cov.web.handle;


import com.alibaba.fastjson.JSON;
import com.cov.web.bean.*;
import com.cov.web.constant.Constants;
import com.cov.web.utils.http.HttpURLConnectionUtil;
import com.cov.web.utils.redis.RedisCache;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Miki
 * @date 2022/4/11 20:35
 * @Information 获取疫情实时数据
 */
@Component
public class DataHandle {

    @Autowired
    private RedisCache redisCache;

    public static final String URL = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5";

    private static final Logger logger = LoggerFactory.getLogger(DataHandle.class);

    // 配置定时执行的注解  支持cron表达式

    @Scheduled(cron = "*/5 * * * * ?") //*/5 * * * * ? 每5秒执行一次,0 */15 * * * ? 每15分钟执行一次
    public void updateData() {
        logger.info("更新数据");
        setCovidRealTimeData();
        setValueName();
    }

    /**
     * 缓存最新疫情数据
     */
    public void setCovidRealTimeData() {
        String respJson = HttpURLConnectionUtil.doGet(URL);
        JSONObject object = JSONObject.fromObject(respJson);
        String data = (String) object.get("data");
        DataBean dataBean = JSON.parseObject(data, DataBean.class);
        redisCache.setCacheObject(Constants.DATA_KEY + "cov.list", dataBean);
    }

    /**
     * 缓存现存确诊和累计确诊数据
     */
    public void setValueName() {
        String respJson = HttpURLConnectionUtil.doGet(URL);

        Gson gson = new Gson();
        Map<?, ?> map = gson.fromJson(respJson, Map.class);

        // 此时增加了一层处理  而且data对应的数据格式是string
        String subStr = (String) map.get("data");
        Map<?, ?> subMap = gson.fromJson(subStr, Map.class);

        ArrayList<?> areaList = (ArrayList<?>) subMap.get("areaTree");
        Map<?, ?> dataMap = (Map<?, ?>) areaList.get(0);
        ArrayList<?> childrenList = (ArrayList<?>) dataMap.get("children");

        List<ChinaMapBean> nowConfirmList = new ArrayList<>();
        List<ChinaMapBean> confirmList = new ArrayList<>();
        for (Object o : childrenList) {
            Map<?, ?> tmp = (Map<?, ?>) o;
            String name = (String) tmp.get("name");

            Map<?, ?> totalMap = (Map<?, ?>) tmp.get("total");
            double nowConfirm = (Double) totalMap.get("nowConfirm");
            double confirm = (Double) totalMap.get("confirm");

            ChinaMapBean nowConfirmBean = new ChinaMapBean(name, (int) nowConfirm);
            ChinaMapBean confirmBean = new ChinaMapBean(name, (int) confirm);
            nowConfirmList.add(nowConfirmBean);
            confirmList.add(confirmBean);
        }
        redisCache.setCacheObject(Constants.DATA_KEY + "cov.now.confirm", nowConfirmList);
        redisCache.setCacheObject(Constants.DATA_KEY + "cov.confirm", confirmList);
    }

    public static void main(String[] args) {
        // getCovidData();
        //getValueName();
    }

}
